﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AudioCore.Biz;
using AudioCore.Entity;

namespace AudioBook.AudioManagement.AudioManage
{
    public partial class MoveArticlePartUC : System.Web.UI.UserControl
    {
        #region Fields & constants

        ArticleBiz bizArticle = new ArticleBiz();

        #endregion Fields & constants

        #region Private methods

        private void LoadArticle()
        {
            int recordCount = 0;
            List<ArticleEntity> lstArticle;

            lstArticle = bizArticle.GetArticleByPaging(hidKeyWord.Value, grid.CurrentPageIndex, grid.PageSize, out recordCount);
            lstArticle.Sort(delegate(ArticleEntity en1, ArticleEntity en2)
            {
                return -en1.ArticleID.CompareTo(en2.ArticleID);
            });

            grid.VirtualItemCount = recordCount;
            grid.DataSource = lstArticle;
            grid.DataBind();
        }

        #endregion
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtFilter.Text.Trim()))
            {
                hidKeyWord.Value = txtFilter.Text.Trim();
                LoadArticle();
            }

            //CommandEventArgs command = new CommandEventArgs(GUIConstants.EventCommand.smx_ShowPopup, null);
            //this.RaiseBubbleEvent(this, command);
        }

        protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ArticleEntity enArticle = (ArticleEntity)e.Item.DataItem;

                Label lblOrder = (Label)e.Item.FindControl("lblOrder");
                lblOrder.Text = (e.Item.ItemIndex + 1).ToString();

                HiddenField hidArticleID = (HiddenField)e.Item.FindControl("hidArticleID");
                hidArticleID.Value = enArticle.ArticleID.ToString();

                HyperLink hplName = (HyperLink)e.Item.FindControl("hplName");
                hplName.Text = enArticle.ArticleName;

                ImageButton btnActive = (ImageButton)e.Item.FindControl("btnActive");
                btnActive.ImageUrl = enArticle.Actived ? GUIConstants.Icon.TrueIcon : GUIConstants.Icon.FalseIcon;

                Label lblPlayCounter = (Label)e.Item.FindControl("lblPlayCounter");
                lblPlayCounter.Text = enArticle.PartCounter.ToString();

                Label lblRate = (Label)e.Item.FindControl("lblRate");
                lblRate.Text = enArticle.RateTimes == 0 ? string.Empty : ((float)enArticle.RatePoint / enArticle.RateTimes).ToString();

                Label lblArticleID = (Label)e.Item.FindControl("lblArticleID");
                lblArticleID.Text = enArticle.ArticleID.ToString();

                ImageButton btnMove = (ImageButton)e.Item.FindControl("btnMove");
                btnMove.CommandName = GUIConstants.EventCommand.MoveArticlePart;
                btnMove.CommandArgument = enArticle.ArticleID.ToString();
            }
        }

        protected void grid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case GUIConstants.EventCommand.MoveArticlePart:
                    CommandEventArgs command = new CommandEventArgs(GUIConstants.EventCommand.MoveArticlePart, int.Parse(e.CommandArgument.ToString()));
                    this.RaiseBubbleEvent(this, command);
                    break;
            }
        }

        protected void grid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grid.CurrentPageIndex = e.NewPageIndex;
            LoadArticle();

            CommandEventArgs command = new CommandEventArgs(GUIConstants.EventCommand.smx_ShowPopup, null);
            this.RaiseBubbleEvent(this, command);
        }
    }
}